package com.djs.utils.maths;

import java.util.*;

/**
 * @Description 对基本数据类型操作的工具类
 * @auther dengjingsi
 * @create 2020-05-21 12:05
 */
public class NumberUtil {
    
    public static void main(String[] args) {
        int binary = 0b111110001;
        System.out.println(binary);
        for (Number a :convertToBinary(binary)){
            System.out.print(a);
        }
    }

    /**
     * 返回number的二进制数(每一位数存在数组里)
     * @param number
     * @return
     */
    public static  int[] convertToBinary(int number){
        switch (number){
            case 0:
                return new int[]{0};
            case 1:
                return new int[]{1};
            case 2:
                return new int[]{1,0};
                default:

        }
        Stack<Integer> binary = new Stack<>();
        int remainder = number%2;
        number = number/2;
        binary.push(remainder);
        while(0 != number){
            binary.push(number%2);
            number = number/2;
        }
        int[] numbers = new int[binary.size()];
        int index = 0;
        while(!binary.empty()){
            numbers[index ++] = binary.pop();
        }
        return numbers;
    }
}
